<?php
/**
 * Created by PhpStorm.
 * User: changshuiwang
 * Date: 2016/7/27
 * Time: 11:30
 */
namespace backend\controllers;


use backend\config\ScoreDef;
use common\models\Score;
use common\service\AuthSrv;
use Yii;
use yii\base\Exception;
use yii\data\ActiveDataProvider;
use yii\data\SqlDataProvider;
use yii\base\Controller;
use yii\data\Sort;

class ScoreController extends Controller
{

    public function beforeAction($action)
    {
        $action = Yii::$app->controller->id.'/'.Yii::$app->controller->action->id;

        if(\Yii::$app->user->can($action)){
            return true;
        }else{
            //$id=Yii::$app->user->getId();
            if(!Yii::$app->user->isGuest){
                echo"<script>alert('对不起，您现在还没获此操作的权限');
                    window.location.href='/site/index';</script>";
            }else{
                echo"<script>alert('对不起，您现在还没获此操作的权限');
                    window.location.href='/site/login';</script>";
                //$this->redirect("/site/login");
            }
            //return false;
            //throw new Exception("..");
        }
    }

    public function actionManage()
    {
        $this->getView()->title="成绩管理";
/*        $role=AuthSrv::getRole(Yii::$app->user->getId());
        switch(key($role)){
            case 'Admin':$where=true;break;
            case 'Teacher':$where=
        }*/
        $count = Yii::$app->db->createCommand('
          SELECT COUNT(*) FROM score')->queryScalar();
        $query="select student.id id,student.tag status,score.score score,score.type type,score.id sc_id
                  from student right join score on student.id=score.stu_id";
        $dataProvider=new SqlDataProvider([
            'sql' => $query,
            'pagination' => [
                'pageSize' => 20,
            ],
            'totalCount' => $count,
            'sort'=>new Sort([
                'attributes' => [
                    'id',
                ],
            ]),
        ]);
        return $this->render("/score/manage",['dataProvider'=>$dataProvider]);
    }

    public function actionAdd()
    {
        if(!empty($_POST)){
            $model=new Score();
            $model->setScenario("add");
            $model->setAttributes($_POST);
            if($model->validate()){
                $error=$model->save();
                if($error){
                    return json_encode(array('status'=>'success'));
                }else{
                    return json_encode(array('status'=>'fail','error'=>$error));
                }
            }else{
                $errors=$model->getErrors();
                while(is_array($errors)){
                    $errors=current($errors);
                }
                return json_encode(array('status'=>"fail",'error'=>$errors));
            }
        }else{
            $this->getView()->title="上传成绩";
            return $this->renderPartial("add");
            //var_dump("11");
        }
    }

    public function actionModify()
    {
        if(!empty($_POST)){

            $model=Score::find()->where(['id'=>$_POST['id']])
                ->andWhere(['stu_id'=>$_POST['stu_id']])->andWhere(['type'=>$_POST['type']])->one();

            $model->setScenario('modify');
            $model->score=$_POST['score'];

            if($model->validate()){
                $error=$model->save();
                if($error){
                    return json_encode(array('status'=>'success'));
                }else{
                    return json_encode(array('status'=>'fail','error'=>$error));
                }
            }else{
                $errors=$model->getErrors();
                while(is_array($errors)){
                    $errors=current($errors);
                }
                return json_encode(array('status'=>'fail','error'=>$errors));
            }
        }else{
            if(isset($_GET['id'])){
                $score=Score::findOne(['id'=>$_GET['id']]);
                if($score){
                    $this->getView()->title="修改成绩";
                    return $this->renderPartial("modify",['score'=>$score]);
                }else{
                    throw new Exception("非法参数");
                }
            }else{
                $this->render("/score/manage");
            }
        }
    }
}